﻿using FlyHorse.LIMS.IBLL;
using FlyHorse.LIMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FlyHorse.LIMS.BLL
{
    public partial class ReportProductBll : BaseBll<ReportProduct>, IReportProductBll
    {
        /// <summary>
        ///根据客户标准创建一个空白的检验结果列表
        /// </summary>
        /// <param name="goodsID"></param>
        /// <param name="customerID"></param>
        /// <returns></returns>
        public List<ReportProductResult> GetEmptResult(string goodsID, string customerID)
        {
            List<ReportProductResult> resList = new List<ReportProductResult>();
            List<StandardCustomer> standardList = this.CurrentDBSession.StandardCustomerDal.LoadEntities(m => m.GoodsID == goodsID && m.CustomerID == customerID ).ToList();
            for (int i = 0; i < standardList.Count; i++)
            {
                ReportProductResult res = new ReportProductResult();
                res.Basis = standardList[i].TestItem.Basis;
                res.ChineseName = standardList[i].TestItem.ChineseName;
                res.FormOrder = standardList[i].FormOrder;
                res.ReportProductResultID = Guid.NewGuid().ToString();
                res.MaxValue = standardList[i].MaxValue;
                res.MinValue = standardList[i].MinValue;
                res.TestItemID = standardList[i].TestItemID;
                res.TestValue = null;
                res.Unit = standardList[i].TestItem.Unit;
                resList.Add(res);
            }
            return resList;
        }

        /// <summary>
        /// 给空的数据列表填充混合样数据
        /// </summary>
        /// <param name="goodsID"></param>
        /// <param name="customerID"></param>
        /// <param name="dateProduct"></param>
        /// <returns></returns>
        public List<ReportProductResult> FillMixResult(string goodsID, string   customerID, DateTime dateProduct)
        {
            List<ReportProductResult> resList = new List<ReportProductResult>();

            List<StandardCustomer> standardList = this.CurrentDBSession.StandardCustomerDal.LoadEntities(m => m.GoodsID == goodsID && m.CustomerID == customerID ).ToList();
            for (int i = 0; i < standardList.Count; i++)
            {
                ReportProductResult res = new ReportProductResult();
                res.Basis = standardList[i].TestItem.Basis;
                res.ChineseName = standardList[i].TestItem.ChineseName;
                res.FormOrder = standardList[i].FormOrder;
                res.ReportProductResultID = Guid.NewGuid().ToString();
                res.MaxValue = standardList[i].MaxValue;
                res.MinValue = standardList[i].MinValue;
               // res.StandardID = standardList[i].StandardCustomerID;
                res.TestValue = null;
                res.Unit = standardList[i].TestItem.Unit;
                resList.Add(res);
            }

            var models = this.CurrentDBSession.ReportSemiDal.LoadEntities(m => m.GoodsID == goodsID && m.SendDate == dateProduct&&m.SamplingPlace.SamplingPlaceName=="合成样");
            if (models.Count() > 1)
            {
                return null;
            }
            else if (models.Count() < 1)
            {
                return resList;
            }
            else  //=1
            {
                ReportSemi report = models.First();
                foreach (var res in resList)
                {
                    string TestItemName = res.ChineseName;
                    foreach (var item in report.ReportSemiResults)
                    {
                        if (item.ChineseName == TestItemName)
                        {
                            res.TestValue = item.TestValue;
                            break;
                        }
                    }
                }
                return resList;
            }


        }

        

        /// <summary>
        /// 删除报告和检验结果
        /// </summary>
        /// <param name="report"></param>
        /// <returns></returns>
        public bool DeleteProducReportAndDetail(ReportProduct report)
        {

            this.CurrentDBSession.Db.Set<ReportProduct>().Remove(report);
            int count =  this.CurrentDBSession.Db.SaveChanges();
            if (count>0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }


    }
}
